﻿using IoTAdapter.Clients.Andon;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
using WCS.Entities;
using WCS.IServices;

namespace WCS.Services.MonitorService
{


    /// <summary>
    /// 输送线监控流程
    /// </summary>
    public class StackerMonitorService : IDeviceMonitor
    {
        public void EvaluationState(TransferPort port)
        {

            #region PLC
            if (port.PlcValue == 0)
            {
                //呼叫
                port.PLcDisplayInfo["Call"] = 0;
                //响应
                port.PLcDisplayInfo["Reponse"] = 0;
                //完成
                port.PLcDisplayInfo["Finished"] = 0;
                //取消
                port.PLcDisplayInfo["ResCancel"] = 0;
            }
            else if(port.PlcValue==1) {
                //呼叫
                port.PLcDisplayInfo["Call"] = 1;
                //响应
                port.PLcDisplayInfo["Reponse"] = 0;
                //完成
                port.PLcDisplayInfo["Finished"] = 0;
                //取消
                port.PLcDisplayInfo["ResCancel"] = 0;
            }
            else if (port.PlcValue == 2)
            {
                //呼叫
                port.PLcDisplayInfo["Call"] = 0;
                //响应
                port.PLcDisplayInfo["Reponse"] = 1;
                //完成
                port.PLcDisplayInfo["Finished"] = 0;
                //取消
                port.PLcDisplayInfo["ResCancel"] = 0;
            }
            else if (port.PlcValue == 3)
            {
                //呼叫
                port.PLcDisplayInfo["Call"] = 0;
                //响应
                port.PLcDisplayInfo["Reponse"] = 0;
                //完成
                port.PLcDisplayInfo["Finished"] = 1;
                //取消
                port.PLcDisplayInfo["ResCancel"] = 0;
            }
            else if (port.PlcValue == 4)
            {
                //呼叫
                port.PLcDisplayInfo["Call"] = 0;
                //响应
                port.PLcDisplayInfo["Reponse"] = 0;
                //完成
                port.PLcDisplayInfo["Finished"] = 0;
                //取消
                port.PLcDisplayInfo["ResCancel"] = 1;
            }


            #endregion

        }

        public void MonitorDevice(Device device)
        {
            // 输送线执行变化
            foreach (var item in device.TransferPorts.Where(x => x.Enable))
            {
                if (item.PLcDisplayInfo.Count == 0)
                {
                    item.PLcDisplayInfo = new Dictionary<string, int>()
                    {
                        // 呼叫
                        ["Call"] = 0,
                        // 完成
                        ["Finished"] = 0,
                        // 取消
                        ["Cancel"] = 0,
                    };
                }


                // 取 PLCAddress 的数据转化成16进制的数据 
                var result = device?.Operation?.ReadInt16(item.PlcAddress);
                //var result1 = device?.Operation?.ReadInt16(item.PlcAddress); result1?.IsSucceed != false
                if (result?.IsSucceed != false)
                {
                    item.PlcValue = result.Value;
                    item.PLcDisplayInfo["Call"] = result.Value;
                    item.PLcDisplayInfo["Finished"] = result.Value;
                    item.PLcDisplayInfo["Cancel"] = result.Value;
                    //var resValues = new List<short>() { result.Value };
                    //resValues.AddRange(result1.Value.Select(p => p));
                    //item.PlcValues = resValues.ToList();
                    EvaluationState(item);
                }
            }
        }
    }
}
